/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.<br/>
 * 项目名称 : 电子商务云服务平台
 * 创建日期 : 2016年6月28日
 * 修改历史 : 
 *     1. [2016年6月30日]修改文件 by chihouliang
 *      添加Javadoc注释，更新代码规范，增加logger日志，代码优化
 */
package com.gavinwind.inspay.common.util;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 
 * 【生成验证码工具类】
 * @date 2016年6月29日 上午11:54:07 
 * @version 1.0
 */
public class RandomValidateCodeUtil {

    //    private static Logger logger = Logger.getLogger(SecurityUtil.class);

    /**放到session中的key*/
    public static final String RANDOM_CODE_KEY = "yzm";
    /**Random对象(产生随机数)*/
    private Random random = new Random();
    /**0-9A-Z字符串，生成验证码使用*/
    private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机产生的字符串
    /**图片宽*/
    private int width = 80;
    /**图片高*/
    private int height = 26;
    /**干扰线数量*/
    private int lineSize = 40;
    /**随机产生字符数量*/
    private int stringNum = 4;

    /*
     * 获得字体
     */
    private Font getFont() {
        return new Font("Fixedsys", Font.CENTER_BASELINE, 18);
    }

    /*
     * 获得颜色
     */
    private Color getRandColor(int cFc, int cBc) {
        if (cFc > 255)
            cFc = 255;
        if (cBc > 255)
            cBc = 255;
        int tR = cFc + random.nextInt(cBc - cFc - 16);
        int tG = cFc + random.nextInt(cBc - cFc - 14);
        int tB = cFc + random.nextInt(cBc - cFc - 18);
        return new Color(tR, tG, tB);
    }

    /**
     * 
     * 生成随机图片
     * @param cRequest 封装的请求对象HttpServletRequest
     * @param cResponse 封装服务器返回对象HttpServletResponse
     */
    public void getRandcode(HttpServletRequest cRequest, HttpServletResponse cResponse) {
        final HttpSession tSession = cRequest.getSession();
        //BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
        BufferedImage tImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics tGraphics = tImage.getGraphics();//产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
        tGraphics.fillRect(0, 0, width, height);
        tGraphics.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));
        tGraphics.setColor(getRandColor(110, 133));
        //绘制干扰线
        for (int i = 0; i <= lineSize; i++) {
            drowLine(tGraphics);
        }
        //绘制随机字符
        String tRandomString = "";
        for (int i = 1; i <= stringNum; i++) {
            tRandomString = drowString(tGraphics, tRandomString, i);
        }
        tSession.removeAttribute(RANDOM_CODE_KEY);
        tSession.setAttribute(RANDOM_CODE_KEY, tRandomString);
        tGraphics.dispose();
        try {
            ImageIO.write(tImage, "JPEG", cResponse.getOutputStream());//将内存中的图片通过流动形式输出到客户端
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 绘制字符串
     */
    private String drowString(Graphics cGraphics, String cRandomString, int cIndex) {
        cGraphics.setFont(getFont());
        cGraphics.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
        String tRand = String.valueOf(getRandomString(random.nextInt(randString.length())));
        cRandomString += tRand;
        cGraphics.translate(random.nextInt(3), random.nextInt(3));
        cGraphics.drawString(tRand, 13 * cIndex, 16);
        return cRandomString;
    }

    /*
     * 绘制干扰线
     */
    private void drowLine(Graphics cGraphics) {
        int tX1 = random.nextInt(width);
        int tY1 = random.nextInt(height);
        int tWidth = random.nextInt(13);
        int tHeight = random.nextInt(15);
        cGraphics.drawLine(tX1, tY1, tX1 + tWidth, tY1 + tHeight);
    }

    /**
     * 
     * 获取0-9A-Z 某个字符 
     * @param cNum 0-9A-Z的字符串所在索引
     * @return 随机字符串
     */
    public String getRandomString(int cNum) {
        return String.valueOf(randString.charAt(cNum));
    }

}
